import tkinter as tk
from tkinter import ttk, filedialog
import pandas as pd
import akshare as ak


root = tk.Tk()
frame_tool = tk.Frame(root)
laber_minute = tk.Label(frame_tool, text="时间间隔")    
enter_minute = tk.Entry(frame_tool)
laber_num = tk.Label(frame_tool, text="查询数量")
enter_num = tk.Entry(frame_tool)
enter_code = tk.Entry(frame_tool)
laber_code = tk.Label(frame_tool, text="股票代码")
button_do = tk.Button(frame_tool, text="查询")
combo_data = ttk.Combobox(frame_tool, values=('实时数据','本地数据'))
#股票数据保存位置
frame_tool2 = tk.Frame(root)
laber_datafile = tk.Label(frame_tool2, text="股票数据保存位置：")
laber_datafile_str = tk.Label(frame_tool2, text="")
datafile = ''
button_file = tk.Button(frame_tool2, text="选择文件", command=lambda: datafile_select())
#计算结果保存位置
frame_tool3 = tk.Frame(root)
laber_savefile = tk.Label(frame_tool3, text="计算结果保存位置：")
laber_savefile_str = tk.Label(frame_tool3, text="")
savefile = ''
button_file2 = tk.Button(frame_tool3, text="选择文件", command=lambda: savefile_select())

def savefile_select():
    global file
    savefile = filedialog.askopenfilename(title='计算结果保存位置')
    global laber_savefile_str
    laber_savefile_str.config(text=savefile)

def datafile_select():
    global file
    datafile = filedialog.askopenfilename(title='股票数据保存位置')
    global laber_datafile_str
    laber_datafile_str.config(text=datafile)

def button_do_command():
    #获取输入内容
    minute = enter_minute.get()
    code = enter_code.get()
    data_type = combo_data.get()
    num = enter_num.get()
    #获取文件路径
    global datafile
    global savefile
    #校验是否全部填写
    if minute == '' or code == '' or num == '':
        tk.messagebox.showerror(title='错误', message='时间间隔和股票代码不能为空！')
        return
    #校验文件路径是否存在
    if datafile == '' or savefile == '':
        tk.messagebox.showerror(title='错误', message='股票数据保存位置和计算结果保存位置不能为空！')
        return
    
    #获取股票数据
    if data_type == '实时数据':
        stock_zh_a_hist_df = ak.stock_zh_a_minute(code,minute)#A股分时历史行情
        data_test = stock_zh_a_hist_df.iloc[-(num+50):-1]
    
    

def init():
    root.title("股票数据查询")
    root.geometry("800x600")
    #工具栏
    frame_tool.pack(side=tk.TOP,pady=10)
    #加入输入框
    laber_minute.pack(side=tk.LEFT)
    enter_minute.pack(side=tk.LEFT)
    laber_num.pack(side=tk.LEFT,padx=20)
    enter_num.pack(side=tk.LEFT,padx=0)
    enter_num.insert(0,"50")#设置默认值
    enter_minute.insert(0,"5")#设置默认值
    laber_code.pack(side=tk.LEFT,padx=20)
    enter_code.pack(side=tk.LEFT,padx=0)
    combo_data.pack(side=tk.LEFT,padx=20)
    combo_data.current(0)
    
    #文件选择
    laber_datafile.pack(side=tk.LEFT)
    laber_datafile_str.pack(side=tk.LEFT)
    button_file.pack(side=tk.LEFT,padx=20)
    frame_tool2.pack(side=tk.TOP,pady=10,padx=0)
    
    #计算结果保存位置
    laber_savefile.pack(side=tk.LEFT)
    laber_savefile_str.pack(side=tk.LEFT)
    button_file2.pack(side=tk.LEFT,padx=20)
    frame_tool3.pack(side=tk.TOP,pady=10,padx=0)
    
    
    #加入按钮
    button_do.configure(command=button_do_command)
    button_do.pack(padx=20)
    
    root.mainloop()

if __name__ == '__main__':
    init()